home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Close Window Thing / Close Window Thing Setup / Close Window Thing Setup.c next >
C/C++ Source or Header  |  2000-06-23  |  11KB  |  462 lines

  1. #define    rMenuBar    128
  2.  
  3. #define    mApple        128
  4. #define    mFile        129
  5.  
  6. #define    iAbout        1
  7.  
  8. #define    iQuit        1
  9.  
  10. typedef struct
  11. {
  12.     short    effectType;
  13.     short    speed;
  14. } Preferences;
  15.  
  16. void InitializeToolbox( void );
  17. void DrawMenubar( void );
  18. void EventOnce( void );
  19. void DrawMenubar( void );
  20. void SetUpMainWindow( void );
  21. void HandleUpdate( EventRecord theEvent );
  22. void HandleMouseDown( EventRecord *eventPtr );
  23. void HandleAppleChoice( short item );
  24. void HandleFileChoice( short item );
  25. void HandleKeyDown( EventRecord theEvent );
  26. void HandleMenuChoice( long theChoice );
  27. void AboutDialog( void );
  28. void DoAddPane( Str255 title );
  29. void CopyString( Str255 to, Str255 from );
  30.  
  31. void OpenPrefs( Preferences *prefs );
  32. void NewPrefs( Preferences *prefs, FSSpec spec );
  33. void SavePrefs( Preferences *prefs );
  34. void ClosePrefs( void );
  35.  
  36. Boolean            done = false;
  37. WindowPtr        mainWindow;
  38. ControlHandle    rootControl, tabControl, pane[2], effectGroup, effect1, effect2, speed;
  39. ControlHandle    speedLabel, slowLabel, fastLabel;
  40. Preferences        **prefsHan, prefs;
  41. short            activePane = 1, prefsFRefNum;
  42.  
  43. void main( void )
  44. {
  45.     InitializeToolbox();
  46.     OpenPrefs( &prefs );
  47.     SetUpMainWindow();
  48.     DrawMenubar();
  49.     
  50.     SetControlValue( speed, prefs.speed );
  51.     
  52.     if ( prefs.effectType != 0 )
  53.         SetControlValue( effectGroup, 1 );
  54.     else
  55.     {
  56.         DeactivateControl( effect1 );
  57.         DeactivateControl( effect2 );
  58.         SetControlValue( effect1, 1 );
  59.     }
  60.     
  61.     if ( prefs.effectType == 1 )
  62.         SetControlValue( effect1, 1 );
  63.     else if ( prefs.effectType == 2 )
  64.         SetControlValue( effect2, 1 );
  65.     
  66.     do
  67.     {
  68.         EventOnce();
  69.     }
  70.     while ( done == false );
  71.     
  72.     prefs.speed = GetControlValue( speed );
  73.     
  74.     prefs.effectType = GetControlValue( effectGroup );
  75.     if ( prefs.effectType == 1 )
  76.     {
  77.         prefs.effectType = GetControlValue( effect1 );
  78.         if ( prefs.effectType == 0 )
  79.             prefs.effectType = 2;
  80.     }
  81.     
  82.     SavePrefs( &prefs );
  83.     ClosePrefs();
  84.     
  85.     KillControls( mainWindow );
  86.     DisposeWindow( mainWindow );
  87. }
  88.  
  89. void InitializeToolbox( void )
  90. {
  91.     InitGraf( &qd.thePort );
  92.     InitFonts();
  93.     InitWindows();
  94.     InitMenus();
  95.     TEInit();
  96.     InitDialogs( 0L );
  97.     FlushEvents( everyEvent, 0 );
  98.     InitCursor();
  99. }
  100.  
  101. void DrawMenubar( void )
  102. {
  103.     Handle        menuBar;
  104.     MenuHandle    menu;
  105.     
  106.     menuBar= GetNewMBar( rMenuBar );
  107.     SetMenuBar( menuBar );
  108.     DrawMenuBar();
  109.     
  110.     menu = GetMenuHandle( mApple );
  111.     AppendResMenu( menu, 'DRVR' );
  112. }
  113.  
  114. void SetUpMainWindow( void )
  115. {
  116.     Rect    windRect = {100,100,320,400}, tabRect, effectGroupRect, effectRect, speedRect;
  117.     Str255    title = "\pEffect Speed:";
  118.     
  119.     mainWindow = NewCWindow( nil, &windRect, "\pClose Window Thing Setup", false, documentProc, 0L, false, nil);
  120.     CreateRootControl( mainWindow, &rootControl );
  121.     
  122.     SetRect( &tabRect, -1, 10, mainWindow->portRect.right + 1, mainWindow->portRect.bottom + 1 );
  123.     
  124.     tabControl = NewControl( mainWindow, &tabRect, "\pTab", true, kControlSupportsEmbedding, 1, 0, kControlTabLargeProc, -1 );
  125.     SetControlValue( tabControl, 0 );
  126.     
  127.     DoAddPane( "\pGeneral" );
  128.     
  129.     speedRect.top = (*pane[0])->contrlRect.top + 12;
  130.     speedRect.left = (*pane[0])->contrlRect.left + 12;
  131.     speedRect.bottom = speedRect.top + 16;
  132.     speedRect.right = (*pane[0])->contrlRect.right - 12;
  133.     speedLabel = NewControl( mainWindow, &speedRect, "\pEffect Speed:", true, 0, 0, 0, kControlStaticTextProc, -1 );
  134.     SetControlData( speedLabel, 0, kControlStaticTextTextTag, title[0], (Ptr)&title[1] );
  135.     EmbedControl( speedLabel, pane[0] );
  136.     
  137.     speedRect.top = speedRect.top + 18;
  138.     speedRect.bottom = speedRect.top + 16;
  139.     speed = NewControl( mainWindow, &speedRect, "\pSpeed", true, 1, 1, 8, kControlSliderProc + kControlSliderHasTickMarks, -1 );
  140.     EmbedControl( speed, pane[0] );
  141.     
  142.     DoAddPane( "\pEffect" );
  143.     effectGroupRect = (*pane[1])->contrlRect;
  144.     effectGroupRect.top += 12;
  145.     effectGroupRect.left += 12;
  146.     effectGroupRect.bottom -= 12;
  147.     effectGroupRect.right -= 12;
  148.     
  149.     effectGroup = NewControl( mainWindow, &effectGroupRect, "\pEnable Close Window Things", true, kControlSupportsEmbedding, 1, 0, kControlGroupBoxCheckBoxProc, -1 );
  150.     SetControlValue( tabControl, 0 );
  151.     EmbedControl( effectGroup, pane[1] );
  152.     
  153.     effectRect = effectGroupRect;
  154.     effectRect.top += 30;
  155.     effectRect.left += 12;
  156.     effectRect.bottom = effectRect.top + 12;
  157.     effectRect.right = effectRect.left + 80;
  158.     effect1 = NewControl( mainWindow, &effectRect, "\pStarburst", true, kControlRadioButtonUncheckedValue, kControlRadioButtonUncheckedValue, kControlRadioButtonCheckedValue, kControlRadioButtonProc, -1 );
  159.     EmbedControl( effect1, effectGroup );
  160.     
  161.     OffsetRect( &effectRect, 0, 17 );
  162.     effect2 = NewControl( mainWindow, &effectRect, "\pSlider", true, kControlRadioButtonUncheckedValue, kControlRadioButtonUncheckedValue, kControlRadioButtonCheckedValue, kControlRadioButtonProc, -1 );
  163.     EmbedControl( effect2, effectGroup );
  164.     
  165.     HideControl( pane[1] );
  166.     
  167.     ShowWindow( mainWindow );
  168. }
  169.  
  170. void EventOnce( void )
  171. {
  172.     EventRecord    event;
  173.     
  174.     WaitNextEvent( everyEvent, &event, 0x7FFFFFFF, nil );
  175.     
  176.     switch ( event.what )
  177.     {
  178.         case mouseDown:
  179.             HandleMouseDown( &event );
  180.             break;
  181.         case keyDown:
  182.             HandleKeyDown( event );
  183.             break;
  184.         case autoKey:
  185.             HandleKeyDown( event );
  186.             break;
  187.         case updateEvt:
  188.             HandleUpdate( event );
  189.             break;
  190.         case activateEvt:
  191.             break;
  192.     }
  193. }
  194.  
  195. void HandleUpdate( EventRecord theEvent )
  196. {
  197.     WindowPtr    window;
  198.     
  199.     window = (WindowPtr)theEvent.message;
  200.     
  201.     BeginUpdate( window );
  202.         SetPort( window );
  203.         DrawControls( window );
  204.     EndUpdate( window );
  205. }
  206.  
  207. void HandleMouseDown( EventRecord *eventPtr )
  208. {
  209.     WindowPtr        whichWindow;
  210.     Rect            dragRect = {0,0,3000, 3000};
  211.     Point            thePoint;
  212.     ControlHandle    theControl;
  213.     short            thePart;
  214.     long            menuChoice;
  215.     
  216.     thePart = FindWindow( eventPtr->where, &whichWindow );
  217.     thePoint = (*eventPtr).where;
  218.     
  219.     switch (thePart)
  220.     {
  221.         case inSysWindow:
  222.             SystemClick( eventPtr, whichWindow );
  223.             break;
  224.         case inMenuBar:
  225.             menuChoice = MenuSelect( eventPtr->where );
  226.             if ( menuChoice != 0 )
  227.                 HandleMenuChoice( menuChoice );
  228.         case inDrag:
  229.             DragWindow( whichWindow, eventPtr->where, &dragRect);
  230.             break;
  231.         case inGoAway:
  232.             if ( TrackGoAway(whichWindow, eventPtr->where) == true)
  233.                 DisposeWindow(whichWindow);
  234.             break;
  235.         case inContent:
  236.             SetPort( whichWindow );
  237.             GlobalToLocal( &thePoint );
  238.             theControl = FindControlUnderMouse( thePoint, whichWindow, &thePart );
  239.             if ( theControl == nil )
  240.                 break;
  241.             
  242.             thePart = HandleControlClick( theControl, thePoint, (*eventPtr).modifiers, (ControlActionUPP)-1L );
  243.             
  244.             if ( theControl == tabControl && thePart != activePane && thePart != 0 )
  245.             {
  246.                 HideControl( pane[activePane - 1] );
  247.                 DrawControls( whichWindow );
  248.                 ShowControl( pane[thePart - 1] );
  249.                 
  250.                 activePane = thePart;
  251.             }
  252.             else if ( theControl == effectGroup && thePart != 0 )
  253.             {
  254.                 if ( GetControlValue( theControl ) == 1 )
  255.                 {
  256.                     SetControlValue( theControl, 0 );
  257.                     DeactivateControl( effect1 );
  258.                     DeactivateControl( effect2 );
  259.                 }
  260.                 else
  261.                 {
  262.                     SetControlValue( theControl, 1 );
  263.                     ActivateControl( effect1 );
  264.                     ActivateControl( effect2 );
  265.                 }
  266.             }
  267.             else if ( theControl == effect1 )
  268.             {
  269.                 SetControlValue( theControl, 1 );
  270.                 SetControlValue( effect2, 0 );
  271.             }
  272.             else if ( theControl == effect2 )
  273.             {
  274.                 SetControlValue( theControl, 1 );
  275.                 SetControlValue( effect1, 0 );
  276.             }
  277.             break;
  278.     }
  279.     HiliteMenu( 0 );
  280. }
  281.  
  282. void HandleAppleChoice( short item )
  283. {
  284.     MenuHandle        appleMenu;
  285.     Str255            accName;
  286.     short            accNumber;
  287.     
  288.     switch ( item )
  289.     {
  290.         case iAbout:
  291.             AboutDialog();
  292.             break;
  293.         default:
  294.             appleMenu = GetMenuHandle( mApple );
  295.             GetMenuItemText( appleMenu, item, accName );
  296.             accNumber = OpenDeskAcc( accName );
  297.             break;
  298.     }
  299. }
  300.  
  301. void HandleFileChoice( short item )
  302. {
  303.     switch ( item )
  304.     {
  305.         case iQuit:
  306.             done = true;
  307.             break;
  308.     }
  309. }
  310.  
  311. void HandleKeyDown( EventRecord theEvent )
  312. {
  313.     short    theChar;
  314.     long    theChoice;
  315.     
  316.     theChar = theEvent.message & charCodeMask;
  317.     
  318.     if ( ( theEvent.modifiers & cmdKey ) != 0 )
  319.     {
  320.         if ( theEvent.what != autoKey )
  321.         {
  322.             theChoice = MenuKey( theChar );
  323.             HandleMenuChoice( theChoice );
  324.         }
  325.     }
  326. }
  327.  
  328. void HandleMenuChoice( long theChoice )
  329. {
  330.     short theMenu;
  331.     short theMenuItem;
  332.     
  333.     theMenu = HiWord( theChoice );
  334.     theMenuItem = LoWord( theChoice );
  335.     switch ( theMenu )
  336.     {
  337.         case mApple:
  338.             HandleAppleChoice( theMenuItem );
  339.             break;
  340.         case mFile:
  341.             HandleFileChoice( theMenuItem );
  342.             break;
  343.     }
  344.     HiliteMenu( 0 );
  345. }
  346.  
  347. void AboutDialog( void )
  348. {
  349.     GrafPtr            savePort;
  350.     DialogPtr        dlog;
  351.     short            item;
  352.     
  353.     GetPort( &savePort );
  354.     
  355.     dlog = GetNewDialog( 128, 0L, (WindowPtr) -1L );
  356.     
  357.     SetDialogDefaultItem( dlog, 1 );
  358.     SetPort( dlog );
  359.     
  360.     if ( dlog )
  361.     {
  362.         do
  363.         {
  364.             ModalDialog( nil, &item );
  365.         }
  366.         while ( item != 1 );
  367.         
  368.         DisposeDialog(dlog);
  369.     }
  370.     
  371.     SetPort( savePort );
  372. }
  373.  
  374. void DoAddPane( Str255 title )
  375. {
  376.     ControlTabInfoRec    theTabInfo;
  377.     Rect                paneRect;
  378.     short                tabCount = GetControlMaximum( tabControl ) + 1;
  379.     
  380.     GetControlData( tabControl, tabCount, kControlTabContentRectTag, sizeof( Rect ), (Ptr)&paneRect, nil );
  381.     
  382.     theTabInfo.version = 0;
  383.     theTabInfo.iconSuiteID = 0;
  384.     SetControlMaximum( tabControl, tabCount );
  385.     
  386.     CopyString( theTabInfo.name, title );
  387.     SetControlData( tabControl, tabCount, kControlTabInfoTag, sizeof( ControlTabInfoRec ), (Ptr)&theTabInfo );
  388.     
  389.     pane[tabCount - 1] = NewControl( mainWindow, &paneRect, "\pTab", true, kControlSupportsEmbedding, 0, 0, kControlUserPaneProc, -1 );
  390.     EmbedControl( pane[tabCount - 1], tabControl );
  391.     SetControlValue( pane[tabCount - 1], 0 );
  392. }
  393.  
  394. void CopyString( Str255 to, Str255 from )
  395. {
  396.     short times = 0;
  397.     
  398.     do
  399.     {
  400.         to[times] = from[times];
  401.         times++;
  402.     }
  403.     while ( times < from[0]+1 );
  404. }
  405.  
  406. void OpenPrefs( Preferences *prefs )
  407. {
  408.     OSErr    myErr;
  409.     FSSpec    mySpec;
  410.     short    myVRef;
  411.     long    myDirID;
  412.     
  413.     myErr = FindFolder(0x8000, 'pref', false, &myVRef, &myDirID);
  414.     myErr = FSMakeFSSpec( myVRef, myDirID, "\pClose Window Thing Prefs", &mySpec );
  415.     
  416.     if( myErr == fnfErr )
  417.         NewPrefs( prefs, mySpec );
  418.     
  419.     prefsFRefNum = FSpOpenResFile( &mySpec,3 );
  420.     
  421.     prefsHan = (Preferences **)GetResource( 'Pref',128 );
  422.     
  423.     prefs->effectType = (**prefsHan).effectType;
  424.     prefs->speed = (**prefsHan).speed;
  425. }
  426.  
  427. void NewPrefs( Preferences *prefs, FSSpec spec )
  428. {
  429.     FSpCreateResFile( &spec, 'PEJ2', 'pref', -1 );
  430.     prefsFRefNum = FSpOpenResFile( &spec, 3 );
  431.     
  432.     prefs->effectType = 1;
  433.     prefs->speed = 1;
  434.     
  435.     (Handle)prefsHan = NewHandle( sizeof( Preferences ) );
  436.     (**prefsHan).effectType = prefs->effectType;
  437.     (**prefsHan).speed = prefs->speed;
  438.     
  439.     HLock( (Handle)prefsHan );
  440.     AddResource( (Handle)prefsHan, 'Pref', 128, "\pPrefs");
  441.     WriteResource( (Handle)prefsHan );
  442.     HUnlock( (Handle)prefsHan );
  443.     
  444.       UpdateResFile( prefsFRefNum );
  445. }
  446.  
  447. void SavePrefs( Preferences *prefs )
  448. {
  449.     (**prefsHan).effectType = prefs->effectType;
  450.     (**prefsHan).speed = prefs->speed;
  451.     
  452.     ChangedResource( (Handle)prefsHan );
  453.       UpdateResFile( prefsFRefNum );
  454. }
  455.  
  456. void ClosePrefs( void )
  457. {
  458.     if ( prefsHan != nil )
  459.         ReleaseResource( (Handle)prefsHan );
  460.     if ( prefsFRefNum )
  461.         FSClose( prefsFRefNum );
  462. }